package com.sbxBase.service.impl;

import com.sbxBase.entity.UserApp;
import com.sbxBase.mapper.AppStoreMapper;
import com.sbxBase.mapper.UserAppMapper;
import com.sbxBase.service.AppStoreService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.entity.sbxBase.AppStore;
import com.common.utils.RequestUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service
public class AppStoreServiceImpl implements AppStoreService {
    @Resource
    private AppStoreMapper appStoreMapper;
    @Resource
    private UserAppMapper userAppMapper;
//    @Resource
//    private UserMapper userMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveStoreApp(AppStore appStore) {
        appStore.setCreated(new Date());
        appStoreMapper.insert(appStore);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAppStoreById(Set<String> appIds) {
        appStoreMapper.deleteBatchIds(appIds);
        //TODO 查看用户是否绑定了该app， 如果绑定则删除，并发送通知
    }


    @Override
    public Page<?> getAppStorePage(AppStore appStore, Integer pageIndex, Integer pageSize) {
        Page<AppStore> appStorePage = new Page<>(pageIndex, pageSize);
        LambdaQueryWrapper<AppStore> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(appStore.getName()), AppStore::getName, appStore.getName());
        wrapper.like(!StringUtils.isEmpty(appStore.getNote()), AppStore::getNote, appStore.getNote());
        wrapper.like(!StringUtils.isEmpty(appStore.getTag()), AppStore::getTag, appStore.getTag());

        return appStoreMapper.selectPage(appStorePage, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUserApp(Set<String> appIds) {

        String loginUserId = RequestUtil.getLoginUserId();

        LambdaQueryWrapper<UserApp> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserApp::getAppId,appIds).eq(UserApp::getUserId,loginUserId);
        List<UserApp> userApps = userAppMapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(userApps)) {
            List<String> appNames = appStoreMapper.selectBatchIds(userApps.stream().map(UserApp::getAppId).collect(Collectors.toList())).stream().map(AppStore::getName).collect(Collectors.toList());
            throw new IllegalArgumentException( appNames+" 应用不能重复添加");
        }
        List<AppStore> appStores = appStoreMapper.selectBatchIds(appIds);
        if (appStores.size() != appIds.size()) {
            for (String appId : appIds) {
                if (appStoreMapper.selectById(appId) == null) {
                    Assert.notNull(appId, "该应用不存在，请从新选择！");
                }
            }
        }
        for (String appId : appIds) {
            UserApp userApp = new UserApp();
            userApp.setCreated(new Date())
                    .setAppId(appId)
                    .setUserId(loginUserId);
            userAppMapper.insert(userApp);
        }

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStoreApp(AppStore appStore) {
        //TODO 没有给修改时间
        appStoreMapper.updateById(appStore);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteUserAppById(Set<String> appId) {
        String loginUserId = RequestUtil.getLoginUserId();
        userAppMapper.delete(new LambdaQueryWrapper<UserApp>().eq(UserApp::getUserId, loginUserId).in(UserApp::getAppId, appId));
    }

    @Override
    public Page<AppStore> getUserAppPage(Integer pageIndex, Integer pageSize) {

        String loginUserId = RequestUtil.getLoginUserId();

        Page<AppStore> appStorePage = new Page<>(pageIndex, pageSize);
        List<UserApp> userApps = userAppMapper.selectList(new LambdaQueryWrapper<UserApp>().eq(UserApp::getUserId, loginUserId));
        if (CollectionUtils.isEmpty(userApps)) {
            return new Page<>();
        }
        List<String> appIds = userApps.stream().map(UserApp::getAppId).collect(Collectors.toList());
        return appStoreMapper.selectPage(appStorePage, new LambdaQueryWrapper<AppStore>().in(AppStore::getId, appIds));
    }
}
